package com.base.thread.demo_29;

import java.util.concurrent.TimeUnit;

public class Demo3 {

	private volatile int signal;
	
	public synchronized int get() {
		System.out.println(Thread.currentThread().getName()+" 开始执行.");
		try {
			wait();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		System.out.println(Thread.currentThread().getName()+" 执行完了.");
		return signal;
	}
	
	public synchronized void set() {
		this.signal = 1;
//		notify(); 
		// notify方法会随机叫醒一个处于wait状态的线程
		  notifyAll();
		System.out.println(Thread.currentThread().getName()+"叫醒线程叫醒之后休眠开始...");
		try {
			TimeUnit.SECONDS.sleep(1);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		System.out.println(Thread.currentThread().getName()+" 执行完了.");
	}
	
	public static void main(String[] args) {
		
		Demo3 demo3 = new Demo3();
		
		Target1 target1 = new Target1(demo3);
		Target2 target2 = new Target2(demo3);
		
		new Thread(target1).start();
		new Thread(target1).start();
		new Thread(target1).start();
		new Thread(target1).start();
		new Thread(target1).start();
		
		
		try {
			TimeUnit.SECONDS.sleep(1);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		
		new Thread(target2).start();
		
		
	}
	
}
